import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;




/*This project will be used to download data from google isssue tracker */
public class google_issue_parser
{
  	String content ="";
  	String title = "";
  	String author = "";
	String date_string = "";
	String description = "";
	int No_of_comments = 0;
	String status = "";
	
  	public void process(String content)
  	{
  		String title = "";
  		int begIndex = content.indexOf(" Issue <a href=\"detail");
  		int midIndex = content.indexOf("\"h3\" >", begIndex);
  		int endIndex = content.indexOf("</span>",midIndex);
  		title = content.substring(midIndex+6, endIndex).trim();
  		
  		begIndex= content.indexOf("td nowrap=\"nowrap\">", begIndex);
  		endIndex = content.indexOf("people starred", begIndex);
  		String star_str = content.substring(begIndex+19, endIndex).trim();
  		int star = Integer.parseInt(star_str);
  		
  		begIndex = content.indexOf("\"userlink\"",begIndex);
  		begIndex =content.indexOf("\">", begIndex);
  		endIndex = content.indexOf("</a",begIndex);
  		String author =content.substring(begIndex+2, endIndex).trim();
  		
  		begIndex = content.indexOf("\"date\" title=\"", endIndex);
  		endIndex = content.indexOf("\">", begIndex);
  		String report_date_string = content.substring(begIndex+18, endIndex);
  		Timestamp reported_timestamp = convert_to_date(report_date_string);
  		
  		begIndex = content.indexOf("<pre>", begIndex);
  		endIndex = content.indexOf("</pre>", begIndex);
  		
  		String description = content.substring(begIndex+5, endIndex);
  		
  		begIndex = content.lastIndexOf("id=\"hc");
  		endIndex = content.indexOf("\"", begIndex+5);
  		String comm_count =content.substring(begIndex+6, endIndex);
  		int numeric_comm_count =Integer.parseInt(comm_count);
  		
  		begIndex = content.indexOf("issuemeta");
  		begIndex = content.indexOf("Status:&nbsp", begIndex);
  		begIndex = content.indexOf("\">", begIndex);
  		begIndex = content.indexOf("\">", begIndex+1);
  		endIndex = content.indexOf("</span>", begIndex);
  		String status = content.substring(begIndex+2, endIndex);
  		
  		String owner = "";
  		begIndex = content.indexOf("Owner:&nbsp;", begIndex);
  		begIndex = content.indexOf("<td>", begIndex);
  		endIndex =content.indexOf("</td>", begIndex);
  		String owner_temp = content.substring(begIndex+6, endIndex).trim();
  		//System.out.println("owner temp:"+owner_temp.trim());
  		if(owner_temp.equalsIgnoreCase("----"))
  		  {
  			// do nothing
  		  }
  		else
  		{
  		 begIndex = content.indexOf("\">", begIndex);
  		 endIndex = content.indexOf("</a", begIndex);
  	     owner = content.substring(begIndex+2, endIndex);
  		}
  		
  		String state = "open";
  		int index = content.indexOf(">Closed:&nbsp;");
  		if(index!=-1)
  		{
  			state = "closed";
  		}
  		
  		String cc = "";
  		
  		index = content.indexOf("Cc:&nbsp;", begIndex);
  		int index2= content.indexOf( "<tr><td colspan=\"2\">", index);
  		while((index!=-1)&&(index<index2))
  		{
  		
  			begIndex = content.indexOf("userlink", index);
  			begIndex = content.indexOf("\">", begIndex);
  			endIndex = content.indexOf("</", begIndex);
  			String temp = content.substring(begIndex+2, endIndex).trim();
  			//System.out.println("temp "+temp+"  \n\n\n-------\n");
  			cc = cc +temp+"\n";
  			index = content.indexOf("userlink", endIndex);
  		}
  		cc=cc.trim();
  		
  		begIndex= content.indexOf("Type-</b>", begIndex);
  		endIndex =content.indexOf("</a>", begIndex);
  		String type = content.substring(begIndex+9, endIndex);
  		
  		 
  		String pri = ""; 
  		String priority = "";
  		
  		begIndex= content.indexOf("Pri-</b>", begIndex);
  		if(begIndex!=-1)
  		{
  		endIndex =content.indexOf("</a>", begIndex);
  		 pri=content.substring(begIndex+8, endIndex);
  		 pri=pri.trim();
  		}
  		else
  		{
  		//System.out.println("begIndex pri="+begIndex);
  		 //if there will be any error in getting priority value it will give value 10 to the priority
  		// Integer. parseInt(prio);*/
  			begIndex= content.indexOf("<b>Priority-</b>", begIndex);
  	  		endIndex =content.indexOf("</a>", begIndex);
  	  	    priority=content.substring(begIndex+16, endIndex);
  	  	    System.out.println("priority:"+priority);
	  		
  		}
  		
  		/*try
  		{
  		
  		 pri=content.substring(begIndex+8, endIndex);
  		 pri=pri.trim();
  		}catch(Exception e)
  		{
  			begIndex= content.indexOf("<b>Priority-</b>", begIndex);
  	  		endIndex =content.indexOf("</a>", begIndex);
  	  	    prio=content.substring(begIndex+16, endIndex);
  		
  	  		System.out.println("priority:"+prio);
  	  		
  	  		//System.exit(0);
  		}*/
  		
  		
  		index = content.indexOf("q=label:Cr", begIndex);
  		//index2= content.indexOf( "<tr><td colspan=\"2\">", index);
  		String area = "";
  		while(index!=-1)
  		{
  		
  			begIndex = content.indexOf("<b>Cr-</b>", index);
  			
  			endIndex = content.indexOf("</a>", begIndex);
  			String temp = content.substring(begIndex+10, endIndex).trim();
  			//System.out.println("temp "+temp+"  \n\n\n-------\n");
  			area = area +temp+",";
  			index = content.indexOf("<b>Cr-</b>", endIndex);
  		}
  	
  		
  		String release_block = "";
  		begIndex= content.indexOf("<b>ReleaseBlock-</b>", begIndex);
  		if(begIndex!=-1)
  		{
  		  endIndex =content.indexOf("</a>", begIndex);
  		  release_block = content.substring(begIndex+20, endIndex);
  		}
  		
  		
  		int mile_index= content.indexOf("class=\"label\"><b>M-", begIndex);
  		String milestone_str="";
  		if(mile_index!=-1)
  		{
  			begIndex =content.indexOf("</b>", mile_index+1);
  			endIndex =content.indexOf("</a>", begIndex);
  			System.out.println("beg Index="+begIndex+" endIndex="+endIndex);
  			milestone_str = content.substring(begIndex+4, endIndex);
  			System.out.println("miilestone_str: "+milestone_str);
  		}
  		
  		
  		
  		//int milestone =Integer.parseInt(milestone_str);
  		
  		//Merge
  		//OS
  		
  		System.out.println("Title= "+title);
  		System.out.println("Star:"+star);
  		System.out.println("Author="+author);
  		System.out.println("Reported date="+report_date_string);
  		System.out.println("Report Timesatmp ="+reported_timestamp);
  		System.out.println("Desc = "+description);
  		System.out.println("comment count="+numeric_comm_count);
  		System.out.println("Owner name="+owner);
  		System.out.println("Status:"+status);
  		System.out.println("State:"+state);
  		System.out.println("cc list:"+cc);
  		System.out.println("Type :"+type);
  		System.out.println("Pri:"+pri);
  		System.out.println("Priority:"+priority);
  		System.out.println("Area:"+area);
  		System.out.println("ReleaseBlock:"+release_block);
  		System.out.println("Milestones:"+milestone_str);
  	}
  	
  	public Timestamp convert_to_date (String reported_date_string)
  	{
  	   DateFormat formatter = new SimpleDateFormat("MMM dd hh:mm:ss yyyy");
       java.util.Date date = null;
	  try 
	  {
		  date = (java.util.Date)formatter.parse(reported_date_string);
	   } catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  }
     // Timestamp timets = new Timestamp(date.getTime());
      
       Timestamp timestamp2= new Timestamp(date.getTime());
       return timestamp2;
  	}
 // This program will be used to download the content of the webpage 	
  	
  public String download(int id)
   {
			String content = "";
			try {
				//URL url = new URL("http://svn.apache.org/viewvc?view=revision&revision=" + revisionID);
				URL url = new URL("https://code.google.com/p/chromium/issues/detail?id=" + id);
				URLConnection conn = url.openConnection();
				conn.setDoOutput(true);
				InputStream in = null;
				in = url.openStream();
				content = pipe(in,"utf-8",id);	
				
			   // System.out.println("content="+content);
			    process(content);
			  
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(0);
			}
			return content;
		
  }/*
  	
	public String download(int id){
		String content = "";
		try {
			
			BufferedReader br =  new BufferedReader(new FileReader("337429.txt"));
			String data;
			while((data = br.readLine())!=null)
			{
				 content =content  + data;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		process(content);
		return content;
	}
  */
	public String pipe(InputStream in,String charset, int revisionID) throws IOException 
	{
		StringBuffer s = new StringBuffer();
		
		if(charset==null||"".equals(charset)){
			charset="utf-8";
		}
		
		String rLine = null;
		BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
		PrintWriter pw = null;
	
		
		while ( (rLine = bReader.readLine()) != null) {
		
			String tmp_rLine = rLine;
			int str_len = tmp_rLine.length();
		
			if (str_len > 0) {
				s.append(tmp_rLine);
				//pw.println(tmp_rLine.replaceAll("\\<.*?>",""));
				//pw.println(tmp_rLine);
				//pw.flush();
			}
	
			tmp_rLine = null;
		}
		
		in.close();	
		//pw.close();
		return s.toString();
		
	}
	
	
  public static void main(String args[])
  {
	google_issue_parser its =  new google_issue_parser();  
	  for(int  i = 2; i <10;i++)
	  {
		  System.out.println("Id = "+i +" \n");
		  its.download(i);
		  
		  System.out.println("\n--------------------------------------------\n");
	  }
	  
  }

}